package com.cmstop.bzgd.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.cmstop.bzgd.R;
import com.cmstop.bzgd.base.BaseActivity;
import com.cmstop.bzgd.beans.ChooseCityBean;
import com.cmstop.bzgd.beans.CountryBean;
import com.cmstop.bzgd.beans.HighRiseCharacterBean;
import com.cmstop.bzgd.beans.HighRiseImage;
import com.cmstop.bzgd.beans.HighRiseNewsInfo;
import com.cmstop.bzgd.beans.HighRiseTypeBean;
import com.cmstop.bzgd.ui.activities.NewesDetailActivity;
import com.cmstop.bzgd.ui.fragment.HighRiseFragment;
import com.cmstop.bzgd.ui.util.IListShowData;
import com.cmstop.bzgd.ui.utils.GlideUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 高层 列表适配器
 */
public class HighRiseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private HighRiseFragment fragment;
    public static final int HIGH_RISE_IMAGE = 1;//高层 图片
    public static final int HIGH_RISE_CHARACTER = 2;//人物
    private List<IListShowData> dataList;
    private Context ctx;
    private OnIntroClickListener introClickListener;
    private OnNewsTypeClickListener newsTypeClickListener;
    private OnNewsItemClickListener newsItemClickListener;
//    private HighRiseCharacterBean temp;

    class ImageViewHolder extends RecyclerView.ViewHolder {

        CustomEXImageView newsImage;
        Context context;

        public ImageViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            newsImage = (CustomEXImageView) view.findViewById(R.id.news_image);

            WindowManager wm = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth();
            int height = width * 9/21 ;
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) newsImage.getLayoutParams();
            params.width = width;
            params.height = height;
            newsImage.setLayoutParams(params);

        }
    }

    class HighRiseCharacterHolder extends RecyclerView.ViewHolder {
        private ImageView characterIcon;
        private CustomFontTextView characterName;
        private CustomFontTextView characterPost;
        private CustomFontTextView characterIntro;
        private LinearLayout specialLayout;
        private RecyclerView newsTypeList;
        private RecyclerView newsList;
        private Context context;

        private CustomFontTextView characterName_no_pic;
        private CustomFontTextView characterPost_no_pic;
        private CustomFontTextView characterIntro_no_pic;

        private LinearLayout top_character_info;
        private LinearLayout top_character_info_no_pic;


        private HighRiseNewsListAdapter newsAdapter;
        private HighRiseTypeAdapter newsTypeAdapter;


        public HighRiseCharacterHolder(Context context, View itemView) {
            super(itemView);
            this.context = context;
            specialLayout = (LinearLayout) itemView.findViewById(R.id.special_layout);
            characterIcon = (ImageView) itemView.findViewById(R.id.top_character_icon);
            characterName = (CustomFontTextView) itemView.findViewById(R.id.character_name);
            characterPost = (CustomFontTextView) itemView.findViewById(R.id.character_post);
            characterIntro = (CustomFontTextView) itemView.findViewById(R.id.character_intro);

            characterName_no_pic = (CustomFontTextView) itemView.findViewById(R.id.character_name_no_pic);
            characterPost_no_pic= (CustomFontTextView) itemView.findViewById(R.id.character_post_no_pic);
            characterIntro_no_pic = (CustomFontTextView) itemView.findViewById(R.id.character_intro_no_pic);

            top_character_info= (LinearLayout) itemView.findViewById(R.id.top_character_info);
            top_character_info_no_pic= (LinearLayout) itemView.findViewById(R.id.top_character_info_no_pic);


            newsTypeList = (RecyclerView) itemView.findViewById(R.id.news_type_list);
            newsList = (RecyclerView) itemView.findViewById(R.id.news_list);
        }


        /**
         * 加载人物的相关新闻列表
         *
         * @param list
         */
        private void init(List<HighRiseNewsInfo> list) {
            LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
            newsList.setLayoutManager(manager);
            newsList.setItemAnimator(new DefaultItemAnimator());
            if (newsAdapter == null) {
                newsAdapter = new HighRiseNewsListAdapter(context, list);//.setItemInternalClick(onHotVideoClick);
                newsList.setAdapter(newsAdapter);
            } else {
                newsAdapter.setDataList(list);
            }

        }

        /**
         * 加载人物的相关新闻列表
         *
         * @param list
         */
        private void initTypeList(List<HighRiseTypeBean> list) {
            if (null != list && list.size() > 0) {
                specialLayout.setVisibility(View.VISIBLE);
                LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
                newsTypeList.setLayoutManager(manager);
                newsTypeList.setItemAnimator(new DefaultItemAnimator());
                if (newsTypeAdapter == null) {
                    newsTypeAdapter = new HighRiseTypeAdapter(context, list);//.setItemInternalClick(onHotVideoClick);
                    newsTypeList.setAdapter(newsTypeAdapter);
                } else {
                    newsTypeAdapter.setDataList(list);
                }
            } else {
                specialLayout.setVisibility(View.GONE);
            }
        }


    }

    public HighRiseAdapter(Context context, HighRiseFragment fragment) {
        ctx = context;
        this.fragment = fragment;
    }


    public void setDataList(List<IListShowData> list) {
        dataList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getDataViewType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view;
        if (viewType == HIGH_RISE_IMAGE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_high_rise_image, parent, false);
            holder = new ImageViewHolder(parent.getContext(), view);
        } else if (viewType == HIGH_RISE_CHARACTER) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_high_rise_character, parent, false);
            holder = new HighRiseCharacterHolder(parent.getContext(), view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        IListShowData data = dataList.get(position);
        if (data.getDataViewType() == HIGH_RISE_IMAGE) {
            HighRiseImage bean = (HighRiseImage) data;
            ImageViewHolder holder1 = (ImageViewHolder) holder;
            if (!TextUtils.isEmpty(bean.getImageUrl())) {
                GlideUtil.getGlid(ctx, bean.getImageUrl()).centerCrop().placeholder(R.mipmap.icon_def_8_3).error(R.mipmap.icon_def_8_3).into(holder1.newsImage);
            }
        } else if (data.getDataViewType() == HIGH_RISE_CHARACTER) {
            final HighRiseCharacterBean temp = (HighRiseCharacterBean) data;
            HighRiseCharacterHolder holder1 = (HighRiseCharacterHolder) holder;
            if (null != temp) {

                if(!TextUtils.isEmpty(temp.getIcon())){
                    holder1.top_character_info.setVisibility(View.VISIBLE);
                    GlideUtil.getGlid(ctx, temp.getIcon()).centerCrop().error(R.mipmap.peng_icon).into(holder1.characterIcon);
                    holder1.top_character_info_no_pic.setVisibility(View.GONE);

                    holder1.characterName.setText(temp.getName());
                    holder1.characterPost.setText(temp.getPost());
                    holder1.characterIntro.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onIntroClick(temp);
                        }
                    });
                    holder1.initTypeList(temp.getNewsTypeList());
                    holder1.init(temp.getNewsList());


                }else{
                    holder1.top_character_info.setVisibility(View.GONE);
                    holder1.top_character_info_no_pic.setVisibility(View.VISIBLE);


                    holder1.characterName_no_pic.setText(temp.getName());
                    holder1.characterPost_no_pic.setText(temp.getPost());
                    holder1.characterIntro_no_pic.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onIntroClick(temp);
                        }
                    });
                    holder1.initTypeList(temp.getNewsTypeList());
                    holder1.init(temp.getNewsList());


                }


            }
        }
    }

    //返回子项个数
    @Override
    public int getItemCount() {
        if (null == dataList) {
            return 0;
        } else {
            return dataList.size();
        }
    }

    public void onIntroClick(HighRiseCharacterBean temp) {
        ArrayList<Pair<String, String>> pairs = new ArrayList<>();
        pairs.add(new Pair<>("页面", "首页"));
        pairs.add(new Pair<>("栏目", "高层"));
        pairs.add(new Pair<>("位置", "人物简介"));
        ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
        tempPair.addAll(pairs);
        tempPair.add(new Pair<>("行为类型", "点击"));

        GridsumWebDissector.getInstance().trackEvent(fragment, "", temp.getName() + " 简介", "", 200, tempPair);

        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> p : pairs) {
            map.put(p.first, p.second);
        }
        //新闻详情
        Intent intent = new Intent(ctx, NewesDetailActivity.class);
        intent.putExtra(BaseActivity.EX_URL, temp.getIntroJson());
        ctx.startActivity(intent);
    }

    public interface OnIntroClickListener {
        void onIntroClick(ChooseCityBean cityState);
    }

    public interface OnNewsTypeClickListener {
        void onNewsTypeClick(ChooseCityBean cityState);
    }

    public interface OnNewsItemClickListener {
        void onNewsItemClick(CountryBean countyState);
    }
}